import { defineStore } from 'pinia';

interface IGroup {
  id: number;
  name: string;
  account?: Array<any>;
  belong: string;
  disabledA: boolean;
  disabledB: boolean;
  [key: string]: any;
}

interface IAccount {
  name: string;
  belong: string;
  disabledA: boolean;
  disabledB: boolean;
  [key: string]: any;
}
interface IPlatform {
  id: number;
  name: string;
  [key: string]: any;
}

export const useOrderStore = defineStore(
  'order',
  {
    state: () => ({
      groups: new Array<IGroup>(),
      users: new Array<IAccount>(),
      platforms: new Array<IPlatform>(),
    }),
    persist: true,
    actions: {
      setGroups(data: any[]) {
        this.groups = data;
      },
      setAccount(data: any[]) {
        this.users = data;
      },
      setPlatforms(data: any[]) {
        this.platforms = data;
      },
      setDisabled(changeDisabledName: string, belongs: string[]) {
        const updateDisabled = (items: any[]) => {
          items.forEach((item) => {
            if (changeDisabledName === 'disabledA') {
              item.disabledA = belongs.includes(item.belong);
            }
            if (changeDisabledName === 'disabledB') {
              item.disabledB = belongs.includes(item.belong);
            }
          });
        };

        updateDisabled(this.groups);
        updateDisabled(this.users);
      },
    },
  },
  // () => {
  //   const groups = ref<any[]>([]);
  //   const users = ref<any[]>([]);
  //   const platforms = ref<any[]>([]);
  //   const setGroups = (data: any[]) => {
  //     groups.value = data;
  //   };

  //   const setAccount = (data: any[]) => {
  //     users.value = data;
  //   };
  //   const setPlatforms = (data: any[]) => {
  //     platforms.value = data;
  //   };

  //   /*
  //    * @changeDisabledName: disabledA | disabledB， 买方选中后，要去设置卖房的disabled属性
  //    * @belongs: group 所属组，同组不允许交易
  //    */
  //   const setDisabled = (changeDisabledName: string, belongs: string[]) => {
  //     const updateDisabled = (items: any[]) => {
  //       items.forEach((item) => {
  //         if (changeDisabledName === 'disabledA') {
  //           item.disabledA = belongs.includes(item.belong);
  //         }
  //         if (changeDisabledName === 'disabledB') {
  //           item.disabledB = belongs.includes(item.belong);
  //         }
  //       });
  //     };

  //     updateDisabled(groups.value);
  //     updateDisabled(users.value);
  //   };
  //   // const setDisabled = (changeDisabledName: string, belongs: string[]) => {
  //   //   groups.value.forEach((item) => {
  //   //     if (changeDisabledName === 'disabledA') {
  //   //       item.disabledA = !!(
  //   //         changeDisabledName === 'disabledA' && belongs.includes(item.belong)
  //   //       );
  //   //     }
  //   //     if (changeDisabledName === 'disabledB') {
  //   //       item.disabledB = !!(
  //   //         changeDisabledName === 'disabledB' && belongs.includes(item.belong)
  //   //       );
  //   //     }
  //   //   });

  //   //   users.value.forEach((item) => {
  //   //     if (changeDisabledName === 'disabledA') {
  //   //       item.disabledA = !!(
  //   //         changeDisabledName === 'disabledA' && belongs.includes(item.belong)
  //   //       );
  //   //     }
  //   //     if (changeDisabledName === 'disabledB') {
  //   //       item.disabledB = !!(
  //   //         changeDisabledName === 'disabledB' && belongs.includes(item.belong)
  //   //       );
  //   //     }
  //   //   });
  //   // };
  //   return {
  //     groups,
  //     users,
  //     platforms,
  //     setGroups,
  //     setAccount,
  //     setPlatforms,
  //     setDisabled,
  //   };
  // },
);
